package com.hu.zhcc.shiro.service.xy;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.lgx.TMemberMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessAnswerMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessQuestionOptionMapper;
import com.hu.zhcc.shiro.dao.xy.TBodyTypeMapper;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.xy.dto.TBodyTypeDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessAnswer;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.pojo.TBodyType;
import com.hu.zhcc.shiro.entity.xy.pojo.TMentalityResult;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TBodyTypeImpl implements TbodyTypeService{

    @Autowired
    private TBodyTypeMapper mapper;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Autowired
    private TMemberMapper tMemberMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TBodyType record) {
        return 0;
    }

    @Override
    @Transactional
    public TBodyTypeDTO insertSelective(TBodyTypeDTO record) {
        TBodyType type = new TBodyType();
        BeanUtils.copyProperties(record,type);
        this.mapper.insertSelective(type);
        record.setId(type.getId());
        return record;
    }

    @Override
    public TBodyTypeDTO selectByPrimaryKey(Integer id) {
        TBodyType type = this.mapper.selectByPrimaryKey(id);
        if (type == null){
            return null;
        }
        TBodyTypeDTO dto = new TBodyTypeDTO();

        BeanUtils.copyProperties(type,dto);

        return dto;
    }

    @Override
    public int updateByPrimaryKeySelective(TBodyTypeDTO record) {
        TBodyType body = new TBodyType();
        BeanUtils.copyProperties(record, body);
        return this.mapper.updateByPrimaryKeySelective(body);
    }

    @Override
    public int updateByPrimaryKey(TBodyType record) {
        return 0;
    }

    @Override
    public Page<TBodyTypeDTO> listBodyType(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.mapper.countBodyType(paremeters);
        List<TBodyTypeDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TBodyType> list = this.mapper.listBodyType(pageRequest);
            dtoList = new ArrayList<TBodyTypeDTO>(list.size());
            for(TBodyType type : list) {
                TBodyTypeDTO dto = new TBodyTypeDTO();
                BeanUtils.copyProperties(type, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TBodyTypeDTO>(total, dtoList);
    }

    @Override
    public List<TBodyTypeDTO> listAllTBodyType() {
        List<TBodyType> list = this.mapper.listAllTBodyType();
        ArrayList<TBodyTypeDTO> dtoList = new ArrayList<>(list.size());
        for(TBodyType body : list) {
            TBodyTypeDTO dto = new TBodyTypeDTO();
            BeanUtils.copyProperties(body, dto);
            dtoList.add(dto);
        }
        return dtoList;
    }

    @Override
    public Boolean queryByMemberId(Integer memberId) {
        TBodyType tBodyType = mapper.queryByMemberId(memberId);
        if(tBodyType==null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Integer addTBodyType(String idCard,TAssessQuestion[][] objects) {
        //通过身份证号找到用户
        TMember member = tMemberMapper.queryByIdCard(idCard);
        TBodyType  tBodyType = new TBodyType();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tBodyType.setCreatetime(simpleDateFormat.format(new Date()));
        tBodyType.setTMemberId(member.getId());
        tBodyType.setName(member.getName());

        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = new TAssessAnswer();
                tAssessAnswer.setUserId(member.getId());
                tAssessAnswer.setQuestionId(tAssessQuestion.getId());
//                tAssessAnswer.setAnswerId();
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getAnswers());
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(Integer.parseInt(tAssessQuestion.getAnswers())).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
                tAssessAnswerMapper.insertSelective(tAssessAnswer);
            }

            if(tAssessQuestions[0].getBankId()==4){
                //阳虚
                tBodyType.setTYadc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==5){
                //阴虚
                tBodyType.setTYidc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==6){
                //气虚
                tBodyType.setTQdc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==7){
                //平和
                tBodyType.setTBc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==8){
                //痰湿
                tBodyType.setTPdc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==9){
                //湿热
                tBodyType.setTDhc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==10){
                //血瘀
                tBodyType.setTBsc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==11){
                //气郁
                tBodyType.setTQsc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==12){
                //特禀质
                tBodyType.setTIsc(markTotal);
            }

        }
        return mapper.insertSelective(tBodyType);

    }
    @Override
    public Integer updateTBodyType(Integer memberId,TAssessQuestion[][] objects) {

        TBodyType tBodyType = mapper.selectByPrimaryKey(memberId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tBodyType.setUpdatetime(simpleDateFormat.format(new Date()));

//        mapper.selectByPrimaryKey()
        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = tAssessAnswerMapper.selectByPrimaryKey(tAssessQuestion.getAnswer_id());
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getAnswers());
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(Integer.parseInt(tAssessQuestion.getAnswers())).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
//                System.out.println(tAssessAnswer);
                tAssessAnswerMapper.updateByPrimaryKeySelective(tAssessAnswer);
            }

            if(tAssessQuestions[0].getBankId()==4){
                //阳虚
                tBodyType.setTYadc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==5){
                //阴虚
                tBodyType.setTYidc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==6){
                //气虚
                tBodyType.setTQdc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==7){
                //平和
                tBodyType.setTBc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==8){
                //痰湿
                tBodyType.setTPdc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==9){
                //湿热
                tBodyType.setTDhc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==10){
                //血瘀
                tBodyType.setTBsc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==11){
                //气郁
                tBodyType.setTQsc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==12){
                //特禀质
                tBodyType.setTIsc(markTotal);
            }

        }
//        System.out.println(tBodyType);
        return mapper.updateByPrimaryKeySelective(tBodyType);

    }
}
